package com.chmingx.JavaBasic.rabbitmq.simple;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.80.130");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        System.out.println("等待接收消息...");
        // 推送的消息如何进行消费的接口回调
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody());
            System.out.println(message);
        };

        // 取消消费的一个回调接口, 如在消费的时候队列被删除掉了
        CancelCallback cancelCallback = (consumerTag) ->{
            System.out.println("消息消费被中断了");
        };
        /*
         * 消费者消费消息
         * 1. 消费哪个队列
         * 2. 消费成功后是否自动挡应答，true自动，false手动
         * 3. 消费者成功消费的回调
         * 4. 消费者未成功消费的回调
         */
        channel.basicConsume("simple", true, deliverCallback, cancelCallback);
    }
}
